// 登录页切换动画
function changeScreen(num) {
  $("#wrap").animate({ left: num + "vw" });
  $("main").animate({ left: -num + "vw" });
};

$(".leftControl").click(function () {
  changeScreen("-100");
});
$(".middleControl").click(function () {
  changeScreen("0");
});
$(".rightControl").click(function () {
  changeScreen("100")
});

// 验证码
function randomAngle() {
  var Range = 45 - -45;
  var Rand = Math.random();
  var num = -45 + Math.round(Rand * Range);
  return num;
}

function randomColor() {
  var colors = ['DarkGreen', 'DarkMagenta', 'DarkOliveGreen', 'Darkorange', 'DarkOrchid', 'DarkRed', 'DarkSalmon', 'DarkSeaGreen', 'DarkSlateBlue', 'DarkSlateGray', 'DarkTurquoise', 'DarkViolet', 'DeepPink', 'DeepSkyBlue', 'DimGray', 'DodgerBlue', 'Feldspar', 'FireBrick', 'Aqua', 'Aquamarine', 'Black', 'Blue', 'BlueViolet', 'Brown', 'BurlyWood', 'CadetBlue', 'Chartreuse', 'Chocolate', 'Coral', 'CornflowerBlue', 'Crimson', 'Cyan'];
  return colors[parseInt(Math.random() * colors.length)]
}

function generateCode() {
  var arr = 'qwertyuiopasdfghjklzxcvbnm1234567890';
  var str = "";
  var answer = "";
  for (var i = 0; i < 4; i++) {
    var tmp = arr[parseInt(Math.random() * arr.length)];
    answer += tmp;
    str += "<span style='transform: rotateZ(" + randomAngle() + "deg);color:" + randomColor() + "'>" + tmp.toUpperCase() + "</span>";
  }
  window.itisasecretcode = answer.toUpperCase();
  return str;
}

$(".verifi").on("click", function () {
  $(this).html(generateCode())
})

// 地址选择
// $("#cityPicker").citySelect({
//   nodata: "none",
//   required: false
// });

// 表单验证

$(function () {
  // 登录表单
  $('#logInForm').formValidation({
    message: ' ',
    framework: 'bootstrap4',
    icon: {
      invalid: 'fa fa-times',
      validating: 'fa fa-refresh'
    },
    fields: {
      // 用户名
      username: {
        validators: {
          notEmpty: {
            message: '用户名不能为空'
          }
        }
      },
      // 密码框
      password: {
        message: ' ',
        validators: {
          notEmpty: {
            message: '密码不能为空'
          }
        }
      },
      // 验证码
      logInVerification: {
        trigger:'keyup',
        validators: {
          notEmpty: {
            message: '验证码不能为空'
          },
          callback: {
            callback: function (value, validator, $field) {
              if (value == '') {
                return {
                  valid: false,
                  message: ' '
                }
              } else if (value.toUpperCase() == window.itisasecretcode) {
                return true;
              } else {
                return {
                  valid: false,
                  message: '验证码不对哦'
                }
              }
            }
          }
        }
      }
    }
  });
  // 普通用户注册
  $('#registNormalForm').formValidation({
    framework: 'bootstrap4',
    icon: {
      validating: 'fa fa-refresh'
    },
    trigger: 'blur',
    fields: {
      // 用户名
      registNormalUsername: {
        validators: {
          notEmpty: {
            message: '用户名不能为空'
          },
          invalid: {
            message: '用户名不对哦'
          }
        }
        
      },
      // 注册手机
      registNormalPhone: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '手机号不能为空'
          },
          phone: {
            country: 'CN',
            message: '这好像不是一个中国大陆手机号哦'
          }
        }
      },
      // 密码框
      registNormalPassword: {
        validators: {
          notEmpty: {
            message: '密码不能为空'
          }
        }
      },
      // 重复密码框
      registNormalRePassword: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '请再次输入您的密码'
          },
          callback: {
            threshold: 1,
            callback: function (value, validator, $field) {
              if (value == '') {
                return {
                  valid: false,
                  message: ' '
                }
              } else if (value == $('#registNormalPassword').val()) {
                return true;
              } else {
                return {
                  valid: false,
                  message: '两次密码不一致哦'
                }
              }
            }
          }
        }
      },
      // 验证问题
      registNormalQuestion: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '验证问题不能为空哦'
          }
        }
      },
      // 验证答案
      registNormalAnswer: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '验证答案不能为空哦'
          }
        }
      },
      // 邮箱
      registNormalEmail: {
        validators: {
          notEmpty: {
            message: '邮箱不能为空'
          },
          emailAddress: {
            message: '这不是一个有效的邮箱哦'
          }
        }
      },
      // 验证码
      registNormalVerification: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '验证码不能为空'
          },
          callback: {
            callback: function (value, validator, $field) {
              if (value == '') {
                return {
                  valid: false,
                  message: ' '
                }
              } else if (value.toUpperCase() == window.itisasecretcode) {
                return true;
              } else {
                return {
                  valid: false,
                  message: '验证码不对哦'
                }
              }
            }
          }
        }
      },
      // 注册须知
      readPropNormal: {
        validators: {
          notEmpty: {
            message: '必须同意注册须知哦'
          },
        }
      },
    }
  });
  // 心理医生注册
  $('#registDoctorForm').formValidation({
    framework: 'bootstrap4',
    icon: {
      validating: 'fa fa-refresh'
    },
    trigger: 'blur',
    fields: {
      // 用户名
      "user.username": {
        validators: {
          notEmpty: {
            message: '用户名不能为空'
          },
          invalid: {
            message: '用户名不对哦'
          }
        }
      },
      // 注册手机
      "user.phone": {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '手机号不能为空'
          },
          phone: {
            country: 'CN',
            message: '这好像不是一个中国大陆手机号哦'
          }
        }
      },
      // 密码框
      "user.password": {
        validators: {
          notEmpty: {
            message: '密码不能为空'
          }
        }
      },
      // 重复密码框
      registDoctorRePassword: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '请再次输入您的密码'
          },
          callback: {
            threshold: 1,
            callback: function (value, validator, $field) {
              if (value == '') {
                return {
                  valid: false,
                  message: ' '
                }
              } else if (value == $('#registDoctorPassword').val()) {
                return true;
              } else {
                return {
                  valid: false,
                  message: '两次密码不一致哦'
                }
              }
            }
          }
        }
      },
      // 邮箱
      "user.email": {
        validators: {
          notEmpty: {
            message: '邮箱不能为空'
          },
          emailAddress: {
            message: '这不是一个有效的邮箱哦'
          }
        }
      },
      // 真实姓名
      "psychologist.realname": {
        validators: {
          notEmpty: {
            message: '请填写您的真实姓名'
          }
        }
      },
      // 身份证号
      "psychologist.identity": {
        validators: {
          notEmpty: {
            message: '请输入您的身份证号'
          },
          id:{
            country:'CN',
            message:'这好像不是中国身份证哦'
          }
        }
      },
      // 学历
      "psychologist.education": {
        validators: {
          notEmpty: {
            message: '请选择您的学历'
          }
        }
      },
      // 地址选择器(需要看文档)
      "psychologist.address": {
        validators: {
          notEmpty: {
            message: '请选择您的城市'
          },
          callback:{
            callback: function (value, validator, $field) {
              var child = $field.parent().children();
              var address = [];
              for(var i =0 ; i <child.length;i++ ){
                address.push(child[i].value);
              }
              // address 用户输入的城市
              return true;
            }
          }
        }
      },
      // 专业资质
      "psychologist.level": {
        validators: {
          notEmpty: {
            message: '请选择您的专业资质'
          }
        }
      },
      // 身份证正面照片
      registDoctorIdFront: {
        validators: {
          notEmpty: {
            message: '请上传您的身份证正面照'
          } , 
          file : {
            type : 'image/jpeg,image/png',
            message: '请上传jpg、jpeg和png格式的图片'
          }
        }
      },
      // 身份证反面照片
      registDoctorIdBack: {
        validators: {
          notEmpty: {
            message: '请上传您的身份证反面照'
          }, 
          file : {
            type : 'image/jpeg,image/png',
            message: '请上传jpg、jpeg和png格式的图片'
          }
        }
      },
      // 心理咨询师证照片
      registDoctorCertifiPic: {
        validators: {
          notEmpty: {
            message: '请上传您的心理咨询师证照'
          }, 
          file : {
            type : 'image/jpeg,image/png',
            message: '请上传jpg、jpeg和png格式的图片'
          }
        }
      },
      // 心理咨询师头像
      registDoctorHeadPic: {
        validators: {
          notEmpty: {
            message: '请上传您的头像'
          }, 
          file : {
            type : 'image/jpeg,image/png',
            message: '请上传jpg、jpeg和png格式的图片'
          }
        }
      },
      // 个人简介
      registDoctorDescribe: {
        trigger: 'keyup blur',
        validators: {
          notEmpty: {
            message: '请填写您的个人简介'
          }
        }
      },
      // 验证码
      registDoctorVerification: {
        trigger: 'keyup',
        validators: {
          notEmpty: {
            message: '验证码不能为空'
          },
          callback: {
            callback: function (value, validator, $field) {
              if (value == '') {
                return {
                  valid: false,
                  message: ' '
                }
              } else if (value.toUpperCase() == window.itisasecretcode) {
                return true;
              } else {
                return {
                  valid: false,
                  message: '验证码不对哦'
                }
              }
            }
          }
        }
      },
      // 注册须知
      readPropDoctor: {
        validators: {
          notEmpty: {
            message: '必须同意注册须知哦'
          },
        }
      },
    }
  });
});
